<?php

class ArticleSeeder extends Seeder {

    public function run() 
    {
		try {
            $articles = Article::all();
            
            // 找出不带article_info的articles，然后添加空白的article_info
            foreach($articles as $article) {
                $article_info = ArticleInfo::find($article->id);
                if (!$article_info) {
                    $article_info = new ArticleInfo();
        			$article_info->id = $article->id;
        			$article_info->created_at = $article->created_at;
        			$article_info->updated_at = $article->updated_at;			
                    $article_info->save();
                    var_dump('saving article info ' . $article->id);
                } else {
                    var_dump('exists article info ' . $article->id);
                }
            }

            $article_infos = ArticleInfo::all();
            // 删除多余的article_info
            foreach($article_infos as $article_info) {
                $article = Article::find($article_info->id);
                if (!$article) {
                    var_dump('deleting article info which doesnt have article ' . $article_info->id);
                    $article_info->delete();
                } else {
                    var_dump('exists article ' . $article_info->id);
                }
            }
		} catch(Exception $ex){
			Log::error($ex);
		}
    }
}